//
//  ViewController.m
//  Objective-cDemo
//
//  Created by ShareAnimation on 2019/8/6.
//  Copyright © 2019 爱丽丝的梦境. All rights reserved.
//

#import "ViewController.h"
#import "DTMemebersView.h"
#import "Masonry.h"
#import "SecondViewController.h"
#import "LEEAlert.h"
#import "DTAlert.h"
#import "YYCategories.h"
#import "NSData+AES.h"
#import "HLDBManager.h"
#import "Person.h"
#import "HLPasswordField.h"
#import "SBPasswordTextField.h"
#import <CommonCrypto/CommonCrypto.h>
#import "HLPaymentController.h"
#import "ThirdViewController.h"
#import "QMUIKit.h"
#import "DTQRScanerController.h"
#import "TOWebViewController.h"
#import "WebViewJavascriptBridge.h"
#import <YBImageBrowser/YBImageBrowser.h>
@interface ViewController ()
@property (strong, nonatomic) DTMemebersView *memberView;

@property (strong, nonatomic) DTAlert *alert;
@property (strong, nonatomic) UITextField *textField;
@property (strong, nonatomic) WebViewJavascriptBridge *webViewBridge;
@property (strong, nonatomic) UITableView *tableView;
@property(nonatomic, strong) QMUIPopupMenuView *popupAtBarButtonItem;
@property(nonatomic, assign) UIStatusBarStyle statusBarStyle;
@property (assign, nonatomic) BOOL statusBarHidden;
@property (strong, nonatomic) UIImageView *imageView;
@end

#define MYDefine [self isIPhoneX]?@"100":@"3333"
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.statusBarStyle = [super preferredStatusBarStyle];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"点击" style:UIBarButtonItemStyleDone target:self action:@selector(doRightAction)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"点击" style:UIBarButtonItemStyleDone target:self action:@selector(doLeftAction)];
    
    [HLDBManager openDB:@"myDB"];
    
    
    UITextField *textField = [UITextField new];
    textField.keyboardType = UIKeyboardTypeDecimalPad;
    textField.backgroundColor = [UIColor yellowColor];
    self.textField = textField;
    [self.view addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.equalTo(@200);
        make.height.equalTo(@50);
    }];
}

- (void)doRightAction {
    
    NSString *resultStr = [self timeAgoWithTimeString:self.textField.text];
    NSLog(@"%@",resultStr);
    
}


/**
 将时间长度换算

 @param string 时间长度
 @return 1秒前，30分钟前，2小时前，50天前
 */
- (NSString *)timeAgoWithTimeString:(NSString *)string {
    int difference = [string intValue];
    int days = difference/(60*60*24);
    int hours = (difference%(60*60*24))/(60*60);
    int minites = (difference%(60*60*24))%(60*60)/60;
    int seconds = (difference%(60*60*24))%(60*60)%60;
    if (days != 0) {
        return [NSString stringWithFormat:@"%d天前",days];
    }else if (hours != 0) {
        return [NSString stringWithFormat:@"%d小时前",hours];
    }else if (minites != 0) {
        return [NSString stringWithFormat:@"%d分钟前",minites];
    }else if (seconds != 0) {
        return [NSString stringWithFormat:@"%d秒前",seconds];;
    }else {
        return @"时间格式有误";
    }
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
    
    if (string.length == 0) {
        return YES;
    }
    
    NSString *checkStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    //正则表达式（只支持两位小数，最高99999.99）
    NSString *regex = @"^\\-?([1-9]\\d{0,4}|0)(\\.\\d{0,2})?$";
    NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicte evaluateWithObject:checkStr];
}

//判断是否为刘海屏，true表示是
- (BOOL)isIPhoneX {
    BOOL iPhoneX = false;
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            iPhoneX = YES;
        }
    }
    return iPhoneX;
}

- (BOOL)isValidMoney:(NSString *)sourceString {
    if (sourceString==nil||sourceString.length<=0) {
        return false;
    }

    NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
    NSString *filterd = [[sourceString componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];
    if (![sourceString isEqualToString:filterd]) {
        return false;
    }
    return true;
}

- (void)doLeftAction {
    CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width-60;
    CGFloat maxHeight = [UIScreen mainScreen].bounds.size.height-100;
    
    [LEEAlert alert].config
    .LeeMaxWidth(maxWidth)
    .LeeMaxHeight(maxHeight)
    .LeeCustomView([self createView])
    .LeeShow();
}

- (UIView *)createView {
    CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width-60;
    CGFloat maxHeight = [UIScreen mainScreen].bounds.size.height-100;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, maxWidth, maxHeight)];
    view.backgroundColor = [UIColor redColor];
    
    return view;
}
@end
